package com.chinasofti.hrm.controller;

import com.chinasofti.hrm.domain.Job;
import com.chinasofti.hrm.domain.User;
import com.chinasofti.hrm.service.HrmService;
import com.chinasofti.hrm.util.tag.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class RegisterController extends BasicController{
    /**
     * 自动注入UserService
     * */
    @Autowired
    @Qualifier("hrmService")
    private HrmService hrmService;
    @RequestMapping(value="/register/registerPage")
    public String registerPage(Model model, Integer pageIndex,
                            @ModelAttribute Job job){

        return "register/register";

    }
    @RequestMapping(value="/register/addUser")
    @ResponseBody
    public Map<String,Object> addUser(@ModelAttribute User user,
                                      ModelAndView mv){
        Map<String,Object> map = new HashMap<>();
        String pass=user.getPassword();
        if(pass!=null && (!pass.trim().equals(""))){
            if(pass.length()<32){
                user.setPassword(this.md5(pass));
            }

        }
        hrmService.addUser(user);
        map.put("msg","注册成功,请关闭登录");
        return map;

    }
}
